﻿/*====================================================================================
* Copyright (c) 2015-2026成都长益西联软件有限公司,All Rights Reserved.
*=====================================================================================
* 文件名称：ParseHelper
* 功能描述：ParseHelper 
* 创 建 者：HFL
* 创建时间：2016-9-19 19:24:57
* 修 改 者：HFL
* 修改时间：2016-9-19 19:24:57
*===================================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MongdbHelper
{
    /// <summary>
    /// 值转换工具类
    /// </summary>
    internal class ParseHelper
    {
        /// <summary>
        /// 日期最大值
        /// </summary>
        public static DateTime DateMax = DateTime.MaxValue;
        /// <summary>
        /// 日期最小值
        /// </summary>
        public static DateTime DateMin = DateTime.Parse("1971-01-01");

        /// <summary>
        /// 日期值转字符串 格式为：yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime datetime)
        {
            return datetime.ToString("yyyy-MM-dd HH:mm:ss");
        }
        /// <summary>
        /// 日期值转字符串 格式为：yyyy-MM-dd
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string DateToString(DateTime datetime)
        {
            return datetime.ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 日期值转字符串 格式为：HH:mm:ss
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string TimeToString(DateTime datetime)
        {
            return datetime.ToString("HH:mm:ss");
        }

        /// <summary>
        /// 转换Int32
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static int Int32Parse(object value)
        {
            return Int32Parse(value, 0);
        }

        /// <summary>
        /// 转换Int32
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static int Int32Parse(object value, int defaultvalue)
        {
            int result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                try
                {
                    result = System.Convert.ToInt32(value.ToString());
                }
                catch
                {
                    result = defaultvalue;
                }
            }
            return result;
        }

        /// <summary>
        /// 转换Int64
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static Int64 Int64Parse(object value)
        {
            return Int64Parse(value, 0);
        }

        /// <summary>
        /// 转换Int64
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static Int64 Int64Parse(object value, Int64 defaultvalue)
        {
            Int64 result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                try
                {
                    result = Int64.Parse(value.ToString().Split('.')[0].Trim());
                }
                catch
                {
                    result = defaultvalue;
                }
            }
            return result;
        }
        /// <summary>
        /// 转换double
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static double DoubleParse(object value)
        {
            return DoubleParse(value, 0);
        }
        /// <summary>
        /// 转换double
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static double DoubleParse(object value, double defaultvalue)
        {
            double result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                try
                {
                    result = double.Parse(value.ToString());
                }
                catch
                {
                    result = defaultvalue;
                }
            }
            return result;
        }

        /// <summary>
        /// 转换Decimal
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static Decimal DecimalParse(object value)
        {
            return DecimalParse(value, 0);
        }

        /// <summary>
        /// 转换Decimal
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static Decimal DecimalParse(object value, Decimal defaultvalue)
        {
            Decimal result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                try
                {
                    result = Decimal.Parse(Decimal.Parse(value.ToString()).ToString("F4").ToString());
                }
                catch
                {
                    result = defaultvalue;
                }
            }
            return result;
        }

        /// <summary>
        /// 转换String
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static string StringParse(object value)
        {
            return StringParse(value, string.Empty);
        }

        /// <summary>
        /// 转换String
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static string StringParse(object value, string defaultvalue)
        {
            string result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                try
                {
                    result = value.ToString();
                }
                catch
                {
                    result = defaultvalue;
                }
            }
            return result;
        }

        /// <summary>
        /// 转换Int16
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static Int16 Int16Parse(object value)
        {
            return Int16Parse(value, (Int16)0);
        }

        /// <summary>
        /// 转换Int16
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static Int16 Int16Parse(object value, Int16 defaultvalue)
        {
            Int16 result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                try
                {
                    result = Int16.Parse(value.ToString());
                }
                catch
                {
                    result = defaultvalue;
                }
            }
            return result;
        }

        /// <summary>
        /// 转换bool默认false
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static bool BitParse(object value)
        {
            return BitParse(value, false);
        }

        /// <summary>
        /// bool转换int 0or1
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static int BooltoInT(object value)
        {
            int result = 0;
            bool values = BoolParse(value);
            if (!values)
            {
                result = 0;
            }
            else
            {
                result = 1;
            }
            return result;
        }

        /// <summary>
        /// 转换bool
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static bool BitParse(object value, bool defaultvalue)
        {
            bool result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                try
                {
                    result = bool.Parse(value.ToString());
                }
                catch
                {
                    result = defaultvalue;
                }
            }
            return result;
        }


        /// <summary>
        /// 转换bool
        /// </summary>      
        /// <returns></returns>
        public static bool IntToBoolParse(Int32 value)
        {
            return value == 0 ? false : true;
        }


        /// <summary>
        /// 转换DateTime
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static DateTime DateTimeParse(object value)
        {
            return DateTimeParse(value, ParseHelper.DateMin);
        }

        /// <summary>
        /// 转换DateTime
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static DateTime DateTimeParse(object value, DateTime defaultvalue)
        {
            DateTime result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                try
                {
                    result = DateTime.Parse(value.ToString());
                }
                catch
                {
                    result = defaultvalue;
                }
            }
            return result;
        }

        /// <summary>
        /// 转换bool 默认返回False
        /// </summary>
        /// <param name="value">obj对象</param>
        public static bool BoolParse(object value)
        {
            return BoolParse(value, false);
        }

        /// <summary>
        /// 转换bool
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static bool BoolParse(object value, bool defaultvalue)
        {
            bool result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                if (bool.TryParse(value.ToString(), out result) == false)
                {
                    result = defaultvalue;
                }
            }
            return result;
        }
        /// <summary>
        ///TimeSpanParse 默认 0:0:0
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <returns></returns>
        public static TimeSpan TimeSpanParse(object value)
        {
            return TimeSpanParse(value, new TimeSpan(0, 0, 0));
        }
        /// <summary>
        /// TimeSpanParse
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static TimeSpan TimeSpanParse(object value, TimeSpan defaultvalue)
        {
            TimeSpan result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                if (TimeSpan.TryParse(value.ToString(), out result) == false)
                {
                    result = defaultvalue;
                }
            }
            return result;
        }



        /// <summary>
        /// int型1,0，转为BOOL值
        /// </summary>
        /// <param name="value">obj对象</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns></returns>
        public static bool IntToBool(object value, bool defaultvalue)
        {
            bool result = defaultvalue;
            if (value != null && value != DBNull.Value)
            {
                int flg = Int32Parse(value, 0);
                if (flg == 0)
                {
                    result = false;
                }
                else if (flg == 1)
                {
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// int型1,0，转为BOOL值
        /// </summary>
        /// <param name="value">需要转换的类型</param>
        /// <returns></returns>
        public static bool IntToBool(object value)
        {
            return IntToBool(value, false);
        }


    }//end class
}